package com.qingxin.wuye.repertory.action;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.store.bean.ChartData;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.repertory.entity.Repertory;
import com.qingxin.wuye.repertory.service.RepertoryService;

@Controller
@RequestMapping("/repertory")
public class RepertoryAction extends BaseAction{
	@Autowired
	private RepertoryService repertoryService;
	
	
	@RequestMapping(value="/lefttree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public List<JsTreeNode> lefttree(String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			/*JsTreeNode root = new JsTreeNode("0", "身体部位","BODY", null,true);
			list.add(root);*/
			JsTreeNode jsTreeNode = new JsTreeNode("0", "选择仓库", "BODY", "id", true);
			jsTreeNode.setCustdata("1");
			list.add(jsTreeNode);
			return list;
		}else{
			list.addAll(repertoryService.getUITreeData(getLoginUser(request),false));
		}
		return list;
	}
	
	
	@RequestMapping(value="/getRepertoryData.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getRepertoryData(HttpServletRequest request){
		return repertoryService.getUITreeData(getLoginUser(request), false);
	}
	
	/**
	 * 跳转列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String index(ModelMap model , HttpServletRequest request) {
		return "wuye/repertory/repertory_list";
	}
	
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String searchname,PageParam pageParam, HttpServletRequest request){
		return repertoryService.list(searchname, pageParam, getLoginUser(request));
	}
	
	
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "wuye/repertory/repertory_add";
	}
	
	

	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid Repertory repertory, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = repertoryService.add(repertory, getLoginUser(request), request);
		return info;
	}
	

	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String repertoryid, HttpServletRequest request){
		Repertory repertory= repertoryService.get(repertoryid);
		model.put("repertory", repertory);
		return "wuye/repertory/repertory_edit";
	}

	@RequestMapping(value="/view.do" , method=RequestMethod.GET)
	public String view(ModelMap model, String repertoryid, HttpServletRequest request){
		Repertory repertory= repertoryService.get(repertoryid);
		model.put("repertory", repertory);
		return "wuye/repertory/repertory_view";
	}
	
	
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid Repertory repertory, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = repertoryService.update(repertory, getLoginUser(request), request);
		return info;
	}
	

	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] repertoryid, HttpServletRequest request){
		if(repertoryid==null || repertoryid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return repertoryService.delete(repertoryid, getLoginUser(request), request);
	}
	
}
